package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.common.Tools;
import com.lucher.mes.dao.ProducescheduleproblemMapper;
import com.lucher.mes.dao.ProductMapper;
import com.lucher.mes.po.Producescheduleproblem;

@Controller
@RequestMapping("/producescheduleproblem")  
public class ProducescheduleproblemController extends BaseController{
	@Autowired
	private ProducescheduleproblemMapper producescheduleproblemDao;
	@Autowired
	private ProductMapper productDao;
	
	/**
	 * 查询信息
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Producescheduleproblem form) {
		return "producescheduleproblem/findProducescheduleproblemList";
	}
	
	/**
	 * 查询信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Producescheduleproblem form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = producescheduleproblemDao.findByCount(form);
		List<Producescheduleproblem> list = producescheduleproblemDao.findByListForPage(form);
		
		for (Producescheduleproblem producescheduleproblem : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			
			objectMap.put("id", producescheduleproblem.getId());
			objectMap.put("prisonercode", producescheduleproblem.getPrisonercode());
			objectMap.put("prisonername", producescheduleproblem.getPrisonername());
			objectMap.put("linecode", producescheduleproblem.getLinecode());
			objectMap.put("linename", producescheduleproblem.getLinename());
			objectMap.put("sectioncode", producescheduleproblem.getSectioncode());
			objectMap.put("sectionname", producescheduleproblem.getSectionname());
			objectMap.put("machinecode", producescheduleproblem.getMachinecode());
			objectMap.put("ordercode", producescheduleproblem.getOrdercode());
			objectMap.put("ordername", producescheduleproblem.getOrdername());
			objectMap.put("producecode", producescheduleproblem.getProducecode());
			objectMap.put("productcode", producescheduleproblem.getProductcode());
			objectMap.put("productname", producescheduleproblem.getProductname());
			objectMap.put("batchno", producescheduleproblem.getBatchno());
			objectMap.put("depotamount", producescheduleproblem.getDepotamount());
			objectMap.put("addtime", StringUtils.isEmpty(producescheduleproblem.getAddtime())?"":Tools.getStr(producescheduleproblem.getAddtime()).substring(0, 19));
			objectMap.put("status", producescheduleproblem.getStatus());
			objectMap.put("problemcode", producescheduleproblem.getProblemcode());
			objectMap.put("problemname", producescheduleproblem.getProblemname());
			objectMap.put("checkercode", producescheduleproblem.getCheckercode());
			objectMap.put("checkername", producescheduleproblem.getCheckername());
			objectMap.put("remark", producescheduleproblem.getRemark());
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}

	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addInit")
	public String addInit(Producescheduleproblem form) {
		return "producescheduleproblem/addProducescheduleproblem";
	}

	/**
	 * 新增
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/save")
	public Map<String,Object>  save(Producescheduleproblem form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getProducecode())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入产品编码");
			return responseJson;
		} 
		
		//保存信息
		Integer producescheduleproblemId = producescheduleproblemDao.insertSelective(form);
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}

	/**
	 * 更新初始化
	 */
	@RequestMapping(value = "/updateInit")
	public String updateInit(Producescheduleproblem form) {
		Producescheduleproblem producescheduleproblem = producescheduleproblemDao.selectByPrimaryKey(form.getId());
		form.setProducescheduleproblem(producescheduleproblem);

		return "producescheduleproblem/updateProducescheduleproblem";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/update")
	public Map<String,Object> update(Producescheduleproblem form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getProductcode())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，产品编码不能为空");
			return responseJson;
		}
		
		// 修改信息
		int flag = producescheduleproblemDao.updateByPrimaryKeySelective(form);
		
		responseJson.put("flag", "1");//
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
    
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/delete")
	public Map<String,Object> delete(Producescheduleproblem form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		//删除材料
		producescheduleproblemDao.deleteByPrimaryKey(form.getId());
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/deleteBatch")
	public Map<String,Object> deleteBatch(@RequestBody List<Producescheduleproblem> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		for (Producescheduleproblem producescheduleproblem : formlist) {
			//删除材料
			producescheduleproblemDao.deleteByPrimaryKey(producescheduleproblem.getId());
		}

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@ResponseBody //获取各问题出现比列
	@RequestMapping(value="/findProblemCountByGrouping")
	public Map<String,Object> findProblemCountByGrouping(Producescheduleproblem record) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		//分组查询各问题数量
		List<Producescheduleproblem> ProblemCountList = producescheduleproblemDao.findProblemCountByGrouping(record);
		for (Producescheduleproblem producescheduleproblem : ProblemCountList) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			objectMap.put("problemname", producescheduleproblem.getProblemname());
			objectMap.put("depotamount", producescheduleproblem.getDepotamount());
			objectlist.add(objectMap);
		}
				
		responseJson.put("data", objectlist);
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
	
}
